package com.junjie.checkin.controller;

import com.junjie.checkin.request.address.AddressRequest;
import com.junjie.checkin.response.Result;
import com.junjie.checkin.response.ResultStatus;
import com.junjie.checkin.response.address.AddressResponse;
import com.junjie.checkin.service.AddressService;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 洪浚杰
 * @description 接收者地址控制类
 * @date 2025/6/16 16:08
 */
@RestController
@RequestMapping("/address")
public class ReceiverAddressController {
    @Autowired
    private AddressService addressService;

    // 添加接收者地址  TODO
    @PostMapping("")
    public Result<AddressResponse> insertAddress(HttpServletRequest request, @RequestBody AddressRequest addressRequest){
        String token = request.getHeader("Authorization");
        AddressResponse response = addressService.insertAddress(token, addressRequest);
        return  Result.getResult(ResultStatus.OK,response);
    }
    // 获取所有地址信息
    @GetMapping("")
    public Result<List<AddressResponse>> getAllAddress(HttpServletRequest request){
        String token = request.getHeader("Authorization");
        List<AddressResponse> responses = addressService.getAllAddress(token);
        return  Result.getResult(ResultStatus.OK,responses);
    }

    // 更新地址信息 TODO
    @PutMapping("{id}")
    public Result updateAddress(@Validated @RequestBody AddressRequest addressRequest,@PathVariable Integer id, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        addressService.updateAddress(addressRequest,token,id);
        return Result.getResult(ResultStatus.OK);
    }
    // 删除地址信息
    @DeleteMapping("/{id}")
    public Result deleteAddress(@PathVariable Integer id, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        addressService.deleteAddress(token,id);
        return Result.getResult(ResultStatus.OK);
    }
}
